<?php
namespace app\common\command;
use app\common\classes\Redis;
use app\common\classes\ReportOrder;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;

class AutoReport extends Command
{
    protected function configure()
    {
        $this->setName('auto_report')
            ->addArgument('action', Argument::OPTIONAL, "方法名称")
            ->setDescription('广告订单自动上报');
    }

    protected function execute(Input $input, Output $output)
    {

        $action = $input->getArgument('action');
        $this->$action();

    }

    /**
     * 刷新比例下标
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function ref_back_link_index(){
        Db::table("pay_order_report_mark_promotion")->where("id",">=",1)->update([
            'all_report_num' => 0,
            'report_num' => 0,
            'skip_num' => 0,
        ]);
    }

    /*
     * 用户注册激活上报
     * 注册当日用户激活的匹配上报
     * */
    public function user_active_reg()
    {
        $links = Db::table('quick_backhaul_link')->where('report_ab',2)->where('status',1)
            ->field('report_ab,status,advert_media_id,channel_id')->select();

        if(empty($links))
        {
            return true;
        }

        $channel_id_gh = array_unique(array_column($links,'channel_id'));

        $previousMinute = strtotime("-1 minute", time()); // 计算上一分钟的时间戳
        $start_date = date('Y-m-d H:i:00', $previousMinute);
        $end_date = date('Y-m-d H:i:59', $previousMinute);

        $where[] = ['channel_id','in',$channel_id_gh];
        $where[] = ['report_status','=',1];
        $where[] = ['regtime','>=',$start_date];
        $where[] = ['regtime','<',$end_date];

        $list = Db::table('quick_user')->where($where)
            ->field('id,advert_media_id,regtime,channel_id,quick_id')
            ->order('id asc')->select();

        if(empty($list))
        {
            return true;
        }

        $ReportOrder    = new ReportOrder();

        foreach ($list as $lv)
        {

            if($lv['advert_media_id'] < 1)
            {
                Db::table('quick_user')->where('id',$lv['id'])->update(['report_status'=>3]);
                continue;
            }

            if($lv['advert_media_id']  == 4)
            {
                //百度不上报激活
                Db::table('quick_user')->where('id',$lv['id'])->update(['report_status'=>3]);
                continue;
            }

            $record = Db::table('quick_backhaul_link')
                    ->where('channel_id',$lv['channel_id'])
                    ->where('advert_media_id',$lv['advert_media_id'])
                    ->where('status',1)
                    ->where('report_ab',2)
                    ->field('')->findOrEmpty();

            if(empty($record))
            {
                Db::table('quick_user')->where('id',$lv['id'])->update(['report_status'=>3]);
                continue;
            }


            $reg_date = date('Ymd',strtotime($lv['regtime']));
            $today_date = date('Ymd');
            if($reg_date != $today_date)
            {
                //非当日注册用户不上报
                Db::table('quick_user')->where('id',$lv['id'])->update(['report_status'=>3]);
                continue;
            }

            $reportResult = $ReportOrder->report_active($lv,$lv['id'],$lv['regtime']);

            if($reportResult['code'] == 100)
            {
                //上报成功
                Db::table('quick_user')->where('id',$lv['id'])->update(['report_status'=>2]);
            }

            if($reportResult['code'] == 103)
            {
                //条件不满足
                Db::table('quick_user')->where('id',$lv['id'])->update(['report_status'=>3]);
            }

            if($reportResult['code'] == 102)
            {
                //上报接口返回失败
                Db::table('quick_user')->where('id',$lv['id'])->update(['report_status'=>4]);
            }
        }
    }

}
